<?php
class MainController extends BaseController {
    /**
     * 应用分类
     */
    function actionCategorys(){}
    /**
     * 热门推荐
     */
    function actionpopular(){}
    /**
     * AiTool工具
     */
    function actionaitools(){}
    /**
     * 助手工具
     */
    function actionHelper(){}
    /**
     * 首页
     */
    function actionIndex(){
      $this->header="媒体审核";
    }
    /**
     * 合集审核
     */
    function actionCollection(){
      $this->header="合集审核";
    }
    /**
     * 言论审核
     */
    function actionComment(){
      $this->header="言论审核";
    }
    /**
     * 认证审核
     */
    function actionCertification(){
      $this->header="认证审核";
    }
    /**
     * 举报处理
     */
    function actionReport(){
      $this->header="举报处理";
    }
    /**
     * 审核规则
     */
    function actionAuditRules(){
      $this->header="审核规则";
    }
    /**
     * 敏感词库管理
     */
    function actionSensitiveWord(){
      $this->header="敏感词库";
    }
    /**
     * 审核工作流
     */
    function actionAuditWorkflow(){
      $this->header="审核工作流";
    }
    /**
     * 审核统计
     */
    function actionAuditStatics(){
      $this->header="审核统计";
    }
    /**
     * 审核人管理
     */
    function actionAuditor(){
      $this->header="审核人管理";
    }
    /**
     * 开发中心API文档页面
     */
    function actionApiDocs(){}
    function findChild($arr,$id){
        $childs=array();
         foreach ($arr as $k => $v){
           if($v['parent_category_id'] == $id){
             $childs[]=$v;
           }
        }
        return $childs;
    }
    function build_tree($rows,$root_id){
          $childs=$this->findChild($rows,$root_id);
          if(empty($childs)){
            return null;
          }
          foreach ($childs as $k => $v){
            $rescurTree=$this->build_tree($rows,$v["category_id"]);
            if( null !=  $rescurTree){ 
            $childs[$k]['children']=$rescurTree;
            }
          }
          return $childs;
      }
    // 接收提交表单
    function actionReceive(){
      // 把提交的数据先dump($_POST)出来看看是良好的习惯。
      
      if(isset($_POST["username"])){
        echo "已经提交了".$_POST["username"];
      }else{
        echo "没有填东东呢";
      }
    }
    /**
     * 加载Amis页面
     */
    function actionPageContent(){
      $this->layout = "";
      // 菜单ID
      $id = arg("page","");
      if(empty($id)){
        // 如果菜单ID不存在，则显示404页面
        $this->display("main_error404.html");
        exit();
      }
      $result = file_get_contents("i/pages/".$id.".json");
      $this->pageJson = $result;
      $this->display("layout_amis.html");
    }
    /**
     * 页面顶部
     */
    function actionHeader(){
      $this->uid = $this->getLoginUserId();
      // 获取未读通知数量
      $notifications = new Notifications();
      $this->unread_notifications = $notifications->findCount(["user_id"=>$this->uid,"is_read"=>0]);
    }
    /**
     * 页面框架初始化
     */
    function actionInitPage(){   
      $this->uid = $this->getLoginUserId();
      if(empty($this->uid)){
        error("请先登录");
      }
      // 获取用户信息
      $user = new Auditor();
      // 查询用户的相关信息
      $userInfo = $user->find(["id"=>$this->uid]);
      // 查询用户的角色信息
      $roleInfo = $user->query("select au_roles.role_name from au_user_roles,au_roles where au_user_roles.role_id=au_roles.role_id and au_user_roles.user_id=".$this->uid);
      // 把用户的角色信息和用户的相关信息合并到一起
      $userInfo["role_name"] = "";
      if(count($roleInfo)>0){
        $userInfo["role_name"] = $roleInfo[0]["role_name"];
      }
      // 读取用户的权限
      $permission = $user->query("SELECT b.permission_key FROM au_role_permissions a INNER JOIN au_user_roles c ON a.role_id = c.role_id INNER JOIN au_permissions b ON a.permission_id = b.permission_id WHERE c.user_id =".$this->uid);
      $userInfo["permission"] = $permission;

      // 获取未读通知数量
      $notifications = new Notifications();
      $userInfo["unread_notifications"] = $notifications->findCount(["user_id"=>$this->uid,"is_read"=>0]);
      // 获取待审核内容总数（媒体内容、合集内容、言论内容、认证内容、举报待处理）
      $media = new MediaContent();
      $userInfo["media_count"] = $media->findCount(["audit_status"=>1]);
      $collection = new Collection();
      $userInfo["collection_count"] = $collection->findCount(["audit_status"=>1]);
      $comment = new Comment();
      $userInfo["comment_count"] = $comment->findCount(["audit_status"=>1]);
      $certification = new Certification();
      $userInfo["certification_count"] = $certification->findCount(["audit_status"=>1]);
      $report = new ReportTo();
      $userInfo["report_count"] = $report->findCount(["handle_status"=>1]);
      // 整体待审核任务数
      $userInfo["total_count"] = $userInfo["media_count"]/1 + $userInfo["collection_count"]/1 + $userInfo["comment_count"]/1 + $userInfo["certification_count"] + $userInfo["report_count"];
      // 输出用户信息
      success($userInfo);
    }
}